<?php
namespace app\api\controller;

use extra\BusinessException;
use extra\Constant;
use think\Log;
use think\Config;
use think\Paginator;
use think\Db;

use app\api\model\MessageTargetUser as MessageTargetUserModel;


class MessageUser extends Base {
    /*
     * app服务端接口获取消息款消息列表
     */
    public function getList(){
        //参数检验
        $request=$this->data['request'];
        $must_param = [
            "page_num"           =>'',// "一页多少条，不传值20条 例如:25",
            "current_page"       =>'',//"当前页",
            "customer_id"        => '',
            "month"              =>''
        ];
        Log::write('MessageUser<>getList<>'.$request['customer_id'].':'.file_get_contents("php://input"));
        
        $month='';
        //参数校验
        paramCheck($request,$must_param);
        //返回数据
        //一页多少条
        $pageNum=(int)$request['page_num'] ? (int)$request['page_num'] : 20;
        //当前页
        $currentPage=(int)$request['current_page'] ? (int)$request['current_page'] : 1;

        $condition=[];
        //推送类别(1:活动链接,2:推送文章)
        (int)$request['customer_id'] ?
            $customerID=(int)$request['customer_id']
            :
            json(['code'=>Constant::ERROR,'msg'=>'the request customer_id is null'])->send();
    
        Log::write('MessageUser<>getList<>'.$request['customer_id'].'<>request:'.json_encode($request));
        $messageBoxModel = new MessageTargetUserModel;
//        $list=$messageBoxModel->getMyMsgList($condition,['list_rows'=>$pageNum,'page'=>$currentPage],['id'=>'desc'],$month);
        if($request['month'] && $currentPage!=1){
            $respose=$messageBoxModel->getMyMsgListMore($customerID,['list_rows'=>$pageNum,'page'=>$currentPage],$month);
//            $respose=$messageBoxModel->getMyMsgListFirst($customerID,['list_rows'=>$pageNum,'page'=>$currentPage],$month);
        
        }else{
            $respose=['respose'=>['list'=>[],'table'=>'']];
            if($currentPage == 1){
                $respose=$messageBoxModel->getMyMsgListFirst($customerID,['list_rows'=>$pageNum,'page'=>$currentPage],$month);
            }
        }
        json(['code'=>Constant::SUCCESS,'msg'=>'success','data'=>['respose'=>$respose['respose'],'request'=>[]]])->send();
    
        
    }
//    public function getListNew(){
//        //参数检验
//        $request=$this->data['request'];
//        $must_param = [
//            "page_num"           =>'',// "一页多少条，不传值20条 例如:25",
//            "current_page"       =>'',//"当前页",
//            "customer_id"        => '',
//            "month"              =>''
//        ];
//        $month='';
//        //参数校验
//        paramCheck($request,$must_param);
//        //返回数据
//        //一页多少条
//        $pageNum=(int)$request['page_num'] ? (int)$request['page_num'] : 20;
//        //当前页
//        $currentPage=(int)$request['current_page'] ? (int)$request['current_page'] : 1;
//
//        $condition=[];
//        //推送类别(1:活动链接,2:推送文章)
//        (int)$request['customer_id'] ?
//            $customerID=(int)$request['customer_id']
//            :
//            json(['code'=>Constant::ERROR,'msg'=>'the request customer_id is null'])->send();
//
//
//        $messageBoxModel = new MessageTargetUserModel;
//        if($request['month']){
////            $list=$messageBoxModel->getMyMsgListMore($customerID,['list_rows'=>$pageNum,'page'=>$currentPage],$month);
//            $respose=$messageBoxModel->getMyMsgListFirst($customerID,['list_rows'=>$pageNum,'page'=>$currentPage],$month);
//
//        }else{
//            $respose=$messageBoxModel->getMyMsgListFirst($customerID,['list_rows'=>$pageNum,'page'=>$currentPage],$month);
//        }
//        json(['code'=>Constant::SUCCESS,'msg'=>'success','data'=>['respose'=>$respose['respose'],'request'=>[]]])->send();
//
//    }
    
    /*
     * 统计点击次数
     */
    public function addClickAmount(){
        
        $request=$this->data['request'];
        $must_param = [
            "customer_id"   =>'',
            "msg_id"        =>'',
            "msg_type"      =>''
        ];
        //参数校验
        paramCheck($request,$must_param);
        //添加阅读量
        $messageTargetUserModel=new MessageTargetUserModel;
        $data=array('m_b_id' => $request['msg_id'],'customer_id' => $request['customer_id'],'click_amount' => '0','forward_amount' => '0','add_time' => '123123123');
        $messageTargetUserModel->addClickAmount($data);
        json(['code'=>Constant::SUCCESS,'msg'=>'success','data'=>[]])->send();
    }
    
    
    
    /*
     * 统计转发次数
     */
    public function addForwardAmount(){
        
        $request=$this->data['request'];
        $must_param = [
            "customer_id"   =>'',
            "msg_id"        =>'',
            "msg_type"      =>''
        ];
        //参数校验
        paramCheck($request,$must_param);
        //添加阅读量
        $messageTargetUserModel=new MessageTargetUserModel;
        $data=array('m_b_id' => $request['msg_id'],'customer_id' => $request['customer_id']);
        $messageTargetUserModel->addForwardAmount($data);
        json(['code'=>Constant::SUCCESS,'msg'=>'success','data'=>[]])->send();
    }
    
    public function getMyNoticeAmount(){
        $request=$this->data['request'];
        $must_param = [
            "customer_id"   =>''
        ];
        //参数校验
        paramCheck($request,$must_param);
        (int)$request['customer_id'] ?
            $customerID=(int)$request['customer_id']
            :
            json(['code'=>Constant::ERROR,'msg'=>'the request customer_id is null'])->send();
    
        $messageTargetUserModel=new MessageTargetUserModel;
        $count=$messageTargetUserModel->getMyNoticeAmount($customerID);
        json(['code'=>Constant::SUCCESS,'msg'=>'success','data'=>['noticeCount'=>$count]])->send();
    }
    
}


















